package com.iims.finance.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.iims.common.annotation.Log;
import com.iims.common.core.controller.BaseController;
import com.iims.common.core.domain.AjaxResult;
import com.iims.common.core.page.TableDataInfo;
import com.iims.common.enums.BusinessType;
import com.iims.finance.domain.CmisCwCollection;
import com.iims.finance.service.ICmisCwCollectionService;
import com.iims.finance.vo.resp.CmisCwCollectionDetailVo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author fanpei
 * @date 2021/7/7 14:17
 */
@Api(tags = "K-收款管理")
//@ApiSort(14)
@RestController
@RequestMapping("/business/collect")
public class CollectionController extends BaseController {

    @Autowired
    private ICmisCwCollectionService collectionService;

    /**
     * 获取收款列表
     */
    @ApiOperation(value = "分页查询收款列表", response = CmisCwCollectionDetailVo.class)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前记录起始索引(从1开始)", name = "pageNum", paramType = "query", required = true),
            @ApiImplicitParam(value = "每页显示记录数（>0）", name = "pageSize", paramType = "query", required = true),
            @ApiImplicitParam(value = "排序列", name = "orderByColumn", paramType = "query"),
            @ApiImplicitParam(value = "升降序方式：0升序 1降序", name = "isAsc", paramType = "query"),
            @ApiImplicitParam(value = "创建开始时间:format = \"yyyy-MM-dd\"", name = "beginTime", paramType = "query"),
            @ApiImplicitParam(value = "创建结束时间:format = \"yyyy-MM-dd\"", name = "endTime", paramType = "query"),
            @ApiImplicitParam(value = "收款方式,值来源于字典business_collect_collectType", name = "collectType", paramType = "query"),
            @ApiImplicitParam(value = "审核状态,值来源于字典business_common_reviewStatus", name = "reviewStatus", paramType = "query"),
            @ApiImplicitParam(value = "收款人:模糊匹配", name = "collectUserCN", paramType = "query"),
            @ApiImplicitParam(value = "审核人:模糊匹配", name = "reviewerCN", paramType = "query")
    })
    @PreAuthorize("@ss.hasPermi('business:collect:list')")
    @GetMapping("/list")
    public TableDataInfo list(@ApiIgnore CmisCwCollection collection) throws Exception {
        startPage();
        setSearchTimeRange(collection);
        setSeqNumPageSizeStart(collection);
        return getDataTable(collectionService.getCwCollectionList(collection));
    }

    @ApiOperation(value = "获取 [合同] 所属收款列表", response = CmisCwCollectionDetailVo.class)
    @GetMapping("/listByContract/{contractId}")
    public AjaxResult listByContract(@ApiParam(value = "正式合同Id", required = true) @PathVariable Long contractId) {
        return AjaxResult.success(collectionService.getContractCwCollectionList(contractId));
    }

    /**
     * 根据收款编号获取详细信息
     */
    @ApiOperation(value = "根据收款Id获取详细信息", response = CmisCwCollectionDetailVo.class)
    @ApiImplicitParam(name = "collectId", value = "收款Id", paramType = "path", required = true)
    @PreAuthorize("@ss.hasPermi('business:collect:query')")
    @GetMapping(value = "/{collectId}")
    public AjaxResult getInfo(@PathVariable Long collectId) {
        return AjaxResult.success(collectionService.getCwCollectionDetailById(collectId));
    }

    /**
     * 新增收款
     */
    @ApiOperation(value = "新增收款")
    @ApiOperationSupport(ignoreParameters = {"collectId","reviewer", "reviewStatus", "reviewTime","reviewerCN","collectUserCN"})
    @PreAuthorize("@ss.hasPermi('business:collect:add')")
    @Log(title = "新增收款", businessType = BusinessType.INSERT)
    @Transactional
    @PostMapping
    public AjaxResult add(@Validated @RequestBody CmisCwCollection collection) {
        collectionService.insertCwCollection(collection);
        return AjaxResult.success();
    }

    /**
     * 修改收款
     */
    @ApiOperation("修改收款")
    @PreAuthorize("@ss.hasPermi('business:collect:edit')")
    @ApiOperationSupport(ignoreParameters = {"proCode", "reviewer", "reviewStatus", "reviewTime","reviewerCN","collectUserCN"})
    @Log(title = "修改收款", businessType = BusinessType.UPDATE)
    @Transactional
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody CmisCwCollection CwCollection) {
        collectionService.updateCwCollection(CwCollection);
        return AjaxResult.success();
    }

    /**
     * 删除收款
     */
    @ApiOperation("删除收款")
    @PreAuthorize("@ss.hasPermi('business:collect:remove')")
    @Log(title = "删除收款", businessType = BusinessType.DELETE)
    @Transactional
    @DeleteMapping("/{collectId}")
    public AjaxResult remove(@ApiParam("收款Id") @PathVariable Long collectId) {
        collectionService.deleteById(collectId);
        return AjaxResult.success();
    }
}
